package com.lm56.web.queryhistory;

import com.lm56.common.MyRequestBody;
import com.lm56.common.MyResponseBody;
import com.lm56.common.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Kelvin Yeung on 2016/11/20.
 */
@Controller
@RequestMapping(value = "/workbench")
public class QueryHistoryController {

    @Autowired
    QueryHistoryService queryHistoryService;

    @RequestMapping(value = "queryHistory", method = RequestMethod.GET)
    public String history() {

        return "workbench/query_history";
    }

    @RequestMapping(value = "queryHistory/histories")
    @ResponseBody
    public MyResponseBody getQueryHistory(@RequestBody MyRequestBody<QueryHistoryQueryForm> myRequestBody) {
        List<QueryHistory> histories;
        Integer totalSize = 0;
        try {
            histories = queryHistoryService.getQueryHistory(myRequestBody);
            totalSize = queryHistoryService.getQueryHistoriesCount(myRequestBody);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return new MyResponseBody(MyResponseBody.STATUS_ERROR, "获取失败");
        }
        MyResponseBody myResponseBody = new MyResponseBody(MyResponseBody.STATUS_SUCESS, "获取成功", histories);
        myResponseBody.setPageSize(myRequestBody.getPageSize());
        myResponseBody.setCurrentPage(myRequestBody.getCurrentPage());
        myResponseBody.setTotalSize(totalSize);
        return myResponseBody;
    }

    @RequestMapping(value = "queryHistory/delete")
    public MyResponseBody deleteQueryHistory(@RequestParam("id") Long id) {
        try {
            queryHistoryService.deleteQueryHistory(id);
        } catch (DataAccessException e) {
            return new MyResponseBody(MyResponseBody.STATUS_ERROR, "删除失败");
        }
        return new MyResponseBody(MyResponseBody.STATUS_SUCESS, "删除成功");
    }


}
